<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-button type="success" plain class="addBtn"  @click="showAdd">添加角色</el-button>
    <!-- 表格 -->
    <el-table
      :data="rolesList"
      style="width: 100%">
      <el-table-column type="expand">
        <template v-slot="{ row }">
          <el-row v-for="l1 in row.children" :key="l1.id" class="l1">
            <el-col :span="4">
              <el-tag type="primary" plain closable @close="delRights(l1.id,row.id)">{{ l1.authName }}</el-tag>
              <i class="el-icon-arrow-right"></i>
            </el-col>
            <el-col :span="20">
              <el-row v-for="l2 in l1.children" :key="l2.id" class="l2">
                <el-col :span="4">
                  <el-tag type="success" plain closable @close="delRights(l2.id,row.id)">{{ l2.authName }}</el-tag>
                  <i class="el-icon-arrow-right"></i>
                </el-col>
                <el-col :span="20">
                  <el-tag v-for="l3 in l2.children" :key="l3.id" type="warning" plain closable class="l3" @close="delRights(l3.id,row.id)">
                    {{ l3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        type="index"
        label="#">
      </el-table-column>
      <el-table-column
        prop="roleName"
        label="角色名称">
      </el-table-column>
      <el-table-column
        prop="roleDesc"
        label="描述">
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作">
        <template v-slot="{ row }">
          <el-button  type="primary" plain icon="el-icon-edit" size="small" @click="showUpdate(row)"></el-button>
          <el-button type="danger" plain icon="el-icon-delete" size="small" @click="delRoles(row.id)"></el-button>
          <el-button type="success" plain icon="el-icon-check" size="small" @click="showAssign(row.id,row)">分配权限</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分配权限模态框 -->
    <el-dialog
      title="分配权限"
      :visible.sync="assignVisible"
      width="40%">

      <!-- 树形控件 -->
      <!-- :props="defaultProps" -->
      <el-tree ref="tree" node-key="id" :data="treeData" :props="defaultProps" show-checkbox></el-tree>

      <span slot="footer" class="dialog-footer">
        <el-button @click="assignVisible = false">取 消</el-button>
        <el-button type="primary" @click="assignRights">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 添加角色模态框 -->
    <el-dialog
      title="添加角色"
      :visible.sync="addVisible"
      width="40%"
      >
      <el-form :model="addForm" :rules="rules" ref="addForm">
        <el-form-item label="角色名称" prop="roleName" label-width="80px">
          <el-input v-model="addForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc" label-width="80px">
          <el-input v-model="addForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoles">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改模态框 -->
    <el-dialog
      title="修改角色"
      :visible.sync="updateVisible"
      width="40%"
      >
      <el-form :model="updateForm" :rules="rules" ref="updateForm">
        <el-form-item label="角色名称" prop="roleName" label-width="80px">
          <el-input v-model="updateForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc" label-width="80px">
          <el-input v-model="updateForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="updateVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateRoles">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rolesList: [],
      assignVisible: false,
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      roleId: '',
      addVisible: false,
      addForm: {
        roleName: '',
        roleDesc: ''
      },
      updateVisible: false,
      updateForm: {
        id: '',
        roleName: '',
        roleDesc: ''
      },
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: ['blur', 'change'] },
          { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: ['blur', 'change'] }
        ],
        roleDesc: [
          { message: '请输入角色描述', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created () {
    this.getRolesList()
  },
  methods: {
    async getRolesList () {
      const { meta, data } = await this.$axios.get('roles')
      if (meta.status === 200) {
        this.rolesList = data
        // console.log(data)
      }
    },
    async delRights (rightId, roleId) {
      // console.log(rightId, roleId)
      const { meta, data } = await this.$axios.delete(`roles/${roleId}/rights/${rightId}`)
      if (meta.status === 200) {
        this.$message.success(meta.msg)
        // console.log(data)
        // 秀啊这个find
        this.rolesList.find(e => e.id === roleId).children = data
      } else {
        this.$message.error(meta.msg)
      }
    },
    async showAssign (id, row) {
      // console.log(id)
      this.roleId = id
      this.assignVisible = true
      const { meta, data } = await this.$axios.get('rights/tree')
      if (meta.status === 200) {
        this.treeData = data
        // console.log(data)
      }
      // console.log(row)
      const arr = []
      row.children.forEach(l1 => {
        l1.children.forEach(l2 => {
          l2.children.forEach(l3 => {
            arr.push(l3.id)
          })
        })
      })
      this.$refs.tree.setCheckedKeys(arr)
      // ///////////
    },
    async assignRights () {
      // 全选半选的id都需要获取到
      const all = this.$refs.tree.getCheckedKeys()
      const half = this.$refs.tree.getHalfCheckedKeys()
      const rids = [...half, ...all].join(',')
      // console.log(rids)
      const { meta } = await this.$axios.post(`roles/${this.roleId}/rights`, { rids })
      if (meta.status === 200) {
        this.assignVisible = false
        this.$message.success(meta.msg)
      } else {
        this.$message.error(meta.msg)
      }
    },
    async delRoles (id) {
      console.log(id)
      const { meta } = await this.$axios.delete(`roles/${id}`)
      if (meta.status === 200) {
        this.$message.success(meta.msg)
        this.getRolesList()
      }
    },
    showAdd () {
      this.addVisible = true
    },
    async addRoles () {
      console.log(222)
      const { meta } = await this.$axios.post('roles', this.addForm)
      console.log(meta)
      if (meta.status === 201) {
        console.log(meta)
        this.$message.success(meta.msg)
        this.addVisible = false
      } else {
        this.$message.error(meta.msg)
      }
    },
    showUpdate (row) {
      // console.log(row)
      this.updateVisible = true
      this.updateForm.id = row.id
      this.updateForm.roleName = row.roleName
      this.updateForm.roleDesc = row.roleDesc
    },
    async updateRoles () {
      // console.log(222)
      const { id, roleName, roleDesc } = this.updateForm
      const { meta } = await this.$axios.put(`roles/${id}`, { roleName, roleDesc })
      if (meta.status === 200) {
        this.$message.success('修改成功')
        this.updateVisible = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.el-breadcrumb{
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #ccc;
}
.addBtn{
  margin: 10px 0;
}
.el-row{

}
.l1{
  border-bottom: 1px dotted #ccc;
  padding-top: 10px;
  padding-bottom: 10px;
}
.l2{
  padding-bottom: 5px;
}
.l3{
  margin-right: 5px;
}
</style>
